let http = require('http')
let server = http.createServer()
let querystring = require('querystring')
server.on('request', (req, res)=>{
    console.log(req.url)
    console.log(req.method)
    let result = []
    req.on('data', function(data){
        result.push(data)
    })
    req.on('end', function(){
        let str = Buffer.concat(result).toString()
        // 如何把字符串转对象
        let contentType = req.headers['content-type']
        let body;
        if(contentType == 'application/x-www-form-urlencoded'){
            body = querystring.parse(str)
        } else if(contentType == 'application/json'){
            body = JSON.parse(str)
        } else {
            body = querystring.parse(str)
        }

        res.end(JSON.stringify(body))
    })
})
server.listen(8080)